$(document).ready(
				function() {
	
					//获取项目名
					var contextPath = document.location.pathname; 
					var index =contextPath.substr(1).indexOf("/"); 
					contextPath = contextPath.substr(0,index+1); 
					
					var totalCount = $("#totalCount").val();
					var pSize = $("#pageSize").val();
					var pageIndex = $("#pageIndex").val();
					var pageCount = $("#pageCount").val();
					

					$("#commDetails").on('hidden.bs.modal', function() {
						$(this).removeData("bs.modal");
					});

					$("#optBtn")
							.delegate(
									'#saveBtn','click',
									function() {
										
										$("#comments").attr("method","post");
										
										var commId = $("#commId").val();
									
											if (commId =="") {
												$("#comments").attr("action","save.do");
											} else {
												$("#comments").attr("action","update.do");
											}
											$("#comments").submit();
										
									});

					$('#pagination2').jqPaginator(
									{
										 /*totalPages: pageCount,
										    visiblePages: 10,
										    currentPage: 1,*/
										totalCounts:totalCount,
										pageSize: pSize,
									    visiblePages: pageCount,
									    currentPage: 1,
										
										    prev: '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
										    page: '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
										    next: '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
										onPageChange : function(num, type) {
											$("#pageIndex").val(num);
											var queryUrl = "queryByPage.do";
											$.ajax({
														url : queryUrl,
														dataType : 'json',
														data : {
															pageIndex :num,
															pageSize : pSize
														},
														cache : false,
														success : function(data) {
															$("#commList tr:not(:first)").remove();
															var results = data.pageResult.resultList;
															for (i = 0; i < results.length; i++) {
																var comments = results[i];
																var rowContent = "";
																if(comments.parentId=="" || comments.parentId==null){
																	rowContent = "<tr class='paretsShow' id='" 
																		+ comments.id
																		+ "'>"
																		+ "<td><input name='commId' id='commId' class='checkbox' type='checkbox' value='"
																		+ comments.id
																		+ "'/></td>"
																		+ "<td>"
																		+ comments.hrtMemberName
																		+ "</td>"
																		+ "<td>"
																		+ comments.gdeName
																		+ "</td>"
																		+ "<td>"
																		+ comments.content
																		+ "</td>"
																		+ "<td>"
																		+ comments.servicerComm
																		+ "</td>"
																		+ "<td>"
																		+ comments.logistics
																		+ "</td>";
																		
																		rowContent = rowContent
																		+ "<td>" 
																		+ "<button id='viewCommentDir' objId='"
																		+ comments.id 
																		+ "' class='btn btn-sm-block' data-toggle='modal' >回复</button>" 
																		+ "</td>"
																		
																		+ "<td>" 
																		+ "<button name='editBtn' id='commBtn' objId='"
																		+ comments.id
																		+ "' class='btn btn-sm-block'>修改</button>" 
																		+ "</td>" 
																		+ "</tr>";
																}
																$("#commList").append(rowContent);
															}
															
														},
														error : function(html) {
															return;
														}
													});
										}
									});

					/**
					 * 全选当前页记录
					 */
					$("#commList").delegate('#selectAll', 'click', function() {
						var isSelectAll = $(this).prop('checked');
						$("input[name='commId']").each(function() {
							$(this).prop("checked", isSelectAll);
						});
					});
					
					/**
					 * 修改按钮事件
					 */
					
					$("#commList").delegate('#commBtn', 'click', function() {
						$("#checks input").removeAttr("readonly");
						$("#checks textarea").removeAttr("readonly");
						
						var commId = $(this).attr("objId");
						var editUrl = "show.do?commId=" + commId;
						$.ajax({
							
							url : editUrl,
							dataType : 'json',
							data : {
								id : commId
							},
							cache : false,
							
							success : function(data) {
								
								loadGI(data);
							},
							error : function(html) {
								alert("Load Data failly");
								refresh();
							}
						});
						
					});
					
					/**
					  删除按钮事件
					 */
					$("#tools").delegate(
						'#deleteBtn',
						'click',
						function() {
							delChk = $('input[name="commId"]:checked');

							if (delChk.length > 0) {
								if (confirm("Are you sure delete the record?")) {

									var ids = "";
									var delUrl = "delete.do";
                                    var idArr=new Array();
                                   
                                    
                                    for (var i = 0; i < delChk.length; i++) {
                                    	idArr.push(delChk.get(i).value);
                               
								}
									
									$.ajax({
												url : delUrl,
												type: "POST",
												data : {
													ids : idArr.toString()
												},
												cache : false,

												success : function(
														html) {
													alert("Delete successfully");
													refresh();
												},
												error : function(
														html) {
													alert("Delete failly");
													refresh();
												}
											});
								} else {
									return;
								}
							} else {
								alert("Please select the record to delete");
								return;
							}
						});
					
					
					/**
					 * 添加
					 */
					$("#tools").delegate('#addComments', 'click', function() {
						
						$("#checks textarea").removeAttr("readonly");
						
						$("#commDetails input").each(function() {
							$(this).prop("value", null);
							$(this).prop("readonly", false);

						});

						$("#commDetails textarea").each(function() {
							$(this).prop("value", null);

						});
						
						$("#commDetails").modal('show');

					});
					
					/**
					 * 修改页面上的默认值
					 */
					function loadGI(comments) {
						$("#commId").val(comments.id);
						/*$("#hrtMemberId").val(comments.hrtMemberId);*/
						$("#hrtMemberId").val(comments.hrtMemberId);
						$("#hrtMemberName").val(comments.hrtMemberName);
						$("#gdeId").val(comments.gdeId);
						$("#gdeName").val(comments.gdeName);
						$("#content").val(comments.content);
						$("#servicerComm").val(comments.servicerComm);
						$("#logistics").val(comments.logistics);

						$("#commDetails").modal('show');
					}

					function vaildate(form) {
						return true;
					}
					/**
					 * 刷新页面
					 * */
					function refresh() {
						window.location.reload();
					}
				});

